package com.northone.statistics.fragment;

import java.util.Calendar;

import com.northone.statistics.BaseFragment;
import com.northone.statistics.R;
import com.northone.statistics.database.SaintDataBaseHelper;
import com.northone.statistics.utils.ToastUtil;
import com.northone.statistics.widget.DragLayout;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

public class MettingFragment extends BaseFragment implements OnClickListener {
	private View view;
	private DragLayout dragLayout;
	private ImageView iv_header;
	private ImageView iv_add;
	private String dayOfWeek;
	private Button btn_start;
	private Button btn_end;

	@SuppressLint("InflateParams")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_metting, null);
		initTitlebar();
		initView();
		initListener();
		return view;
	}

	private void initTitlebar() {
		iv_header = (ImageView) view.findViewById(R.id.iv_lefticon);
		iv_add = (ImageView) view.findViewById(R.id.iv_add);
		iv_add.setImageResource(R.drawable.saint_titlebar_add_selector);
		iv_add.setVisibility(View.VISIBLE);
		TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
		tv_title.setText(getResources().getString(R.string.metting));

	}

	private void initView() {
	}

	private void initListener() {
		iv_header.setOnClickListener(this);
		iv_add.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_lefticon:
			dragLayout.open();
			break;
		case R.id.iv_add:
			addMetting();
			break;
		case R.id.btn_startTime:
		case R.id.btn_endTime:
			choiceTimeDialog(v);
			break;
		default:
			break;
		}
	}

	/** 选择日期与时间的对话框 。 */
	private void choiceTimeDialog(View v) {
		View view = View.inflate(activity, R.layout.dialog_datetime_picker, null);
		final DatePicker date_picker = (DatePicker) view.findViewById(R.id.date_picker);
		final TimePicker time_picker = (TimePicker) view.findViewById(R.id.time_picker);
		AlertDialog.Builder timeDialog = new AlertDialog.Builder(activity);
		timeDialog.setView(view);
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(System.currentTimeMillis());
		date_picker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null);
		time_picker.setIs24HourView(true);
		time_picker.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY));
		time_picker.setCurrentMinute(cal.get(Calendar.MINUTE));

		if (v.getId() == R.id.btn_startTime) {
			timeDialog.setTitle("选择开始时间");
			timeDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					StringBuffer sb = new StringBuffer();
					sb.append(String.format("%d-%02d-%02d ", date_picker.getYear(), date_picker.getMonth() + 1,
							date_picker.getDayOfMonth()));
					sb.append(time_picker.getCurrentHour() + ":" + time_picker.getCurrentMinute());

					btn_start.setText("开始时间：" + sb.toString());
					dialog.cancel();
				}
			});
		} else if (v.getId() == R.id.btn_endTime) {
			timeDialog.setTitle("选择结束时间");
			timeDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					StringBuffer sb = new StringBuffer();
					sb.append(String.format("%d-%02d-%02d ", date_picker.getYear(), date_picker.getMonth() + 1,
							date_picker.getDayOfMonth()));
					sb.append(time_picker.getCurrentHour() + ":" + time_picker.getCurrentMinute());

					btn_end.setText("结束时间：" + sb.toString());
					dialog.cancel();
				}
			});
		}
		timeDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		timeDialog.create().show();
	}

	/** 添加聚会 。 */
	private void addMetting() {
		View dialog_view = View.inflate(activity, R.layout.dialog_view_addmetting, null);
		final EditText et_name = (EditText) dialog_view.findViewById(R.id.et_name);
		final EditText et_place = (EditText) dialog_view.findViewById(R.id.et_place);
		final EditText et_remark = (EditText) dialog_view.findViewById(R.id.et_remark);
		Spinner sp_week = (Spinner) dialog_view.findViewById(R.id.sp_week);
		btn_start = (Button) dialog_view.findViewById(R.id.btn_startTime);
		btn_end = (Button) dialog_view.findViewById(R.id.btn_endTime);
		btn_end.setOnClickListener(this);
		btn_start.setOnClickListener(this);

		sp_week.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				dayOfWeek = getResources().getStringArray(R.array.dayOfWeek)[position];
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});
		new AlertDialog.Builder(activity)
				// 设置View视图 。
				.setView(dialog_view).setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						ContentValues values = new ContentValues();
						String mettName = et_name.getText().toString();
						if (TextUtils.isEmpty(mettName)) {
							ToastUtil.showToast(activity, "请输入聚会信息");
							return;
						}else{
							values.put("name", mettName);
						}
						values.put("DayOfWeek", dayOfWeek);
						values.put("StartTime", btn_start.getText().toString());
						values.put("EndTime", btn_end.getText().toString());
						values.put("Place", et_place.getText().toString());
						values.put("Remark", et_remark.getText().toString());
						application.database.insert(SaintDataBaseHelper.TABLE_NAME_METTING, null, values);
					}
				}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}).create().show();
	}
}
